import { request } from "./http";
import moment from "moment";

export const projectAPI = {
  projectList(query: object) {
    return request({
      method: "GET",
      url: `project/page`,
      query
    });
  },

  projectAdd(params: object) {
    return request({
      method: "POST",
      url: `project/create`,
      data: params
    });
  },

  projectDel(params: object) {
    return request({
      method: "POST",
      url: `project/delete`,
      data: params
    });
  },

  getUserSummary(params: object) {
    return request({
      method: "GET",
      url: `project/get-project-user-summary`,
      query: params
    });
  },

  /**
   * @desc 24H 接口调用次数
   * @param params
   */
  get24hGatewayCallCount(params: any) {
    const startTime = moment().subtract(24, 'hour').unix() * 1000;
    return request({
      method: "GET",
      url: `project/get-project-gateway-call-count`,
      query: {
        ...params,
        startTime,
      },
    });
  },

  /**
   * @desc 24h PV count
   * @param params
   */
  get24hPvCount(params: any) {
    const startTime = moment().subtract(24, 'hour').unix() * 1000;
    return request({
      method: "GET",
      url: `project/get-project-page-view-count`,
      query: {
        ...params,
        startTime
      },
    });
  },

  /**
   * @desc 4h PV metrics
   * @param params
   */
  getProject4hPageView(params: any) {
    const startTime = moment().subtract(4, 'hour').unix() * 1000;
    return request({
      method: "GET",
      url: `project/get-project-page-view-metrics`,
      query: {
        ...params,
        startTime
      },
    });
  },

  /**
   * @desc 24h error count
   * @param params
   */
  get24hErrorCount(params: { app_key: string, type: string }) {
    const url = params.type === 'error'
      ? 'get-project-error-count'
      : 'get-project-api-error-count';
    const startTime = moment().subtract(24, 'hour').unix() * 1000;
    return request({
      method: "GET",
      url: `project/${url}`,
      query: {
        app_key: params.app_key,
        startTime
      },
    });
  },
};
